const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const isDev = process.env.NODE_ENV === 'development'
const config = {
  target: 'web',
  mode: process.env.NODE_ENV,
  entry: {
    app: path.join(__dirname, 'src/main.js')
  },
  output: {
    filename: '[name].bundle.[hash].js',
    path: path.join(__dirname, 'dist')
  },
  devtool: 'inline-source-map',
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: ['vue-loader']
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader', 'postcss-loader']
        })
      },
      {
        test: /\.(gif|jpg|jpeg|png|svg|)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 1024
            }
          }
        ]
      },
      {
        enforce: 'pre',
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader'
      },
      {
        test: /\.(vue|js|jsx)$/,
        loader: 'eslint-loader',
        exclude: /node_modules/,
        // 预处理
        enforce: 'pre',
        options: {
          formatter: require('eslint-friendly-formatter')
        }
      }
    ]
  },
  plugins: [
    // make sure to include the plugin!
    // new webpack.DefinePlugin({
    //   'process.env': {
    //     NODE_ENV: isDev ? 'development' : 'production'
    //   }
    // }),
    new BundleAnalyzerPlugin(),
    new VueLoaderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new HtmlWebpackPlugin({ template: './src/index.html' }),
    new ExtractTextPlugin('styles.[hash:8].css')

  ]
}
if (isDev) {
  config.devServer = {
    contentBase: './dist',
    host: '127.0.0.1',
    port: '8080',
    hot: true,
    overlay: {// 显示webpack的错误
      errors: true
    }
  }
} else {
  config.plugins.push(new CleanWebpackPlugin())
}

module.exports = config
